package com.huafeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafeng.commonutil.CheckUtils;
import com.huafeng.mapper.SysLoginLogMapper;
import com.huafeng.model.base.Result;
import com.huafeng.model.entity.SysLoginLog;
import com.huafeng.model.entity.SysOperLog;
import com.huafeng.model.vo.SysLoginLogQueryVo;
import com.huafeng.service.SysLoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 花风
 * @date 2023/8/19 10:26
 * @Description SysLoginLogServiceImpl
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {
    @Override
    public Result queryLoginLogByPage(SysLoginLogQueryVo sysLoginLogQueryVo) {
        // 获取参数
        int page = sysLoginLogQueryVo.getPage();
        int limit = sysLoginLogQueryVo.getLimit();
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();

        // 创建分页对象
        Page<SysLoginLog> sysLoginLogPage = new Page<>(page, limit);
        // 创建查询对象
        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        Map<String,String> params = new HashMap<>();
        params.put("username",username);
        params.put("createTimeBegin",createTimeBegin);
        params.put("createTimeEnd",createTimeEnd);
        queryWrapper = CheckUtils.checkParams(queryWrapper,sysLoginLogQueryVo,params);

        // 分页条件查询
        sysLoginLogPage = page(sysLoginLogPage, queryWrapper);
        return Result.ok(sysLoginLogPage);
    }

    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);
        sysLoginLog.setStatus(status);

        this.save(sysLoginLog);
    }
}
